import request from "../../utils/request"
Page({
  data: {
    switch1Checked: false,
    seatArr: [],
    seat: '',
    name: '',
    aroundList: [],
    copyList: [],
    firstNum: [],
    dinner: false,
    dinnerList: [],
    dinnerCopyList: []
  },
  switch1Change() {
    // 展示周围的人
    const switch1Checked = !this.data.switch1Checked
    this.setData({
      switch1Checked: switch1Checked
    })
    // 先判断页面类型
    if (this.data.dinner) {
      
      const regex = /\d+/g;
      const result = this.data.seat.match(regex);
      const numbers = result.map(num => parseInt(num));
      // 判断是否点击了展示周围人
      if (this.data.switch1Checked) {
        const listCopy1 = this.data.dinnerList
        const listCopy2 = JSON.parse(JSON.stringify(this.data.dinnerCopyList));
        for (var i = 0; i < listCopy1.length; i++) {
          listCopy1[i] = listCopy2[i]
          // for (var j = 0; j< listCopy1[i].list.length; j++){
          //   listCopy1[i].list[j].querywh_num +="号"
          // }
        }
        this.setData({
          dinnerList:listCopy1
        })
      }else{
        const listCopy = this.data.dinnerList
        // console.log(numbers)
        for (var i = 0; i < listCopy.length; i++) {
          for (var j = 0; j< listCopy[i].list.length; j++){
            if(listCopy[i].dinner_num == numbers[0]){
              if(listCopy[i].list[j].querywh_num == numbers[1]){

              }else{
                listCopy[i].list[j].name = ''
                listCopy[i].list[j].querywh_num = ''
              }
            }else{
              listCopy[i].list[j].name = ''
              listCopy[i].list[j].querywh_num = ''
            }
          }
        }
        this.setData({
          dinnerList:listCopy
        })
      }


    } else {


      
      if (this.data.switch1Checked) {
        const regex2 = /\d+/g;
        const arrList = this.data.seatArr
        for (var i = 0; i < this.data.aroundList.length; i++) {
          const result2 = this.data.aroundList[i].seat.match(regex2);
          const numbers = result2.map(num => parseInt(num));
          // console.log(numbers,'123213'); // 输出：["3", "2"]
          const indexNum = (numbers[0] - 1) * 6 + numbers[1] - 1
          arrList[indexNum] = this.data.aroundList[i].name;
        }
        this.setData({
          seatArr: arrList
        })
        // aroundList
      } else {
        const regex2 = /\d+/g;
        const arrList = this.data.seatArr
        for (var i = 0; i < this.data.aroundList.length; i++) {
          const result2 = this.data.aroundList[i].seat.match(regex2);
          const numbers = result2.map(num => parseInt(num));
          // console.log(numbers,'123213'); // 输出：["3", "2"]
          const indexNum = (numbers[0] - 1) * 6 + numbers[1] - 1
          arrList[indexNum] = '';
        }
        const firstNum = this.data.firstNum
        const indexNum2 = (firstNum[0] - 1) * 6 + firstNum[1] - 1
        arrList[indexNum2] = this.data.name;
        this.setData({
          seatArr: arrList
        })
      }
    }


    console.log(this.data.aroundList)
  },
  onLoad(options) {
    let res = JSON.parse(options.param1)
    console.log(res);
    if (res.dinner_num) {
      // 数组深拷贝
      const newArray = JSON.parse(JSON.stringify(res.list));
      this.setData({
        dinner: true,
        dinnerList: res.list,
        name: res.name,
        seat: res.seat,
        dinnerCopyList: newArray
      })
      const regex = /\d+/g;
      const result = this.data.seat.match(regex);
      const numbers = result.map(num => parseInt(num));
      const listCopy = this.data.dinnerList
      // console.log(numbers)
      // 去除掉除本人以外的数据
      for (var i = 0; i < listCopy.length; i++) {
        for (var j = 0; j< listCopy[i].list.length; j++){
          if(listCopy[i].dinner_num == numbers[0]){
            if(listCopy[i].list[j].querywh_num == numbers[1]){

            }else{
              listCopy[i].list[j].name = ''
              listCopy[i].list[j].querywh_num = ''
            }
          }else{
            listCopy[i].list[j].name = ''
            listCopy[i].list[j].querywh_num = ''
          }
        }
      }
      this.setData({
        dinnerList:listCopy
      })
    } else {
      const arr = new Array(84).fill('');
      // console.log(arr.length);
      this.setData({
        dinner: false,
        seatArr: arr,
        name: res.name,
        seat: res.seat,
        aroundList: res.list
      })
      // 获取本人的位置信息
      const regex = /\d+/g;
      const result = this.data.seat.match(regex);
      const numbers = result.map(num => parseInt(num));
      console.log(numbers, '123213'); // 输出：["3", "2"]
      this.setData({
        firstNum: numbers
      })
      // 插入静态页面中的位置
      const indexNum = (numbers[0] - 1) * 6 + numbers[1] - 1
      arr[indexNum] = res.name;
      const newArray = JSON.parse(JSON.stringify(arr));
      this.setData({
        seatArr: arr,
        copyList: newArray
      })
    }

  }
});